package com.yjps.system.controller;

import com.alibaba.fastjson.JSON;
import com.yjps.common.message.Message;
import com.yjps.common.page.Pager;
import com.yjps.system.model.Permission;
import com.yjps.system.model.Role;
import com.yjps.system.model.User;
import com.yjps.system.service.PermissionService;
import com.yjps.system.service.RoleService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;
import java.util.Set;

/**
 * Created by jessy on 2015/8/17.
 */
@Controller
@RequestMapping("/system/permission")
public class PermissionController {
    private static final Logger log = LoggerFactory.getLogger(PermissionController.class);

    @Autowired
    PermissionService permissionService;
    @Autowired
    RoleService roleService;

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public String list(Model model,HttpServletRequest request,HttpServletResponse response) throws Exception {
        Pager pager = permissionService.getPermissionByPager(new Pager<Permission>(request, response));
        model.addAttribute(pager);
        return "system/permission/list";
    }






    @RequestMapping(value = "/edit",method = RequestMethod.GET)
    public String editForm(@RequestParam(value = "id", required = false)String id,Model model){
        if(!StringUtils.isEmpty(id)){
            Permission permission = permissionService.findPermission(Integer.parseInt(id));
            if(permission != null){
                model.addAttribute("permission", permission);
            }
        }
        return "system/permission/edit";
    }
    @RequestMapping(value = "/edit",method = RequestMethod.POST)
    public String edit(Permission permission,HttpServletRequest request,Model model){
        if(permission != null) {
            Message message = null;
            if (StringUtils.isEmpty(permission.getId()) || permission.getId()==0) {
                if (permissionService.findPermissionByName(permission.getPermissionName()) != null) {
                    model.addAttribute("permission", permission);
                    message = new Message(false, "the permissionname has exist");
                    return "system/permission/edit";
                }
                message = new Message(true,"add permission success");
                permissionService.addPermission(permission);

            }else {
                message = new Message(true,"update permission success");
                permissionService.updatePermission(permission);
                log.debug("update role " + permission.getPermissionName() + " success");
            }
                    model.addAttribute("message", message);
        }
        return "redirect:/system/permission/list";
    }

    @RequestMapping(value = "/delete",method = RequestMethod.GET)
    public String delete(@RequestParam(value = "id",required = true)String id,Model model){
        Message message = null;
        Permission permission = permissionService.findPermission(Integer.parseInt(id));
        if(permission != null) {
            List<Role> roleList = roleService.roleList();
            for(Role r:roleList){
               Set<String > permissionSet = r.getPermissionsName();
                if(permissionSet.contains(permission.getPermissionName())){
                    log.debug("删除已有角色使用的权限不成功");
                    message = new Message(false,"不能删除该权限");
                    model.addAttribute("message", message);
                    return "redirect:/system/permission/list";
                }
            }
        }
            permissionService.deletePermisson(Integer.parseInt(id));
        message = new Message(true,"delete successfully ");
        model.addAttribute("message",message);
        return "redirect:/system/permission/list";
    }
    @ResponseBody
    @RequestMapping(value = "/getPermission",method = RequestMethod.GET)
    public String getRole(@RequestParam(value = "id",required = true)String id){
        Permission p = permissionService.findPermission(Integer.parseInt(id));
        String jsonString  = JSON.toJSONString(p);
        System.out.println("permission json string is " +jsonString);
        return jsonString;
    }
}
